#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition.hpp>
#include <iostream>

#include "RobotsDownloadInterface.h"

boost::mutex RobotsDownloadInterface::myMutex;
boost::condition RobotsDownloadInterface::cond;
unsigned int RobotsDownloadInterface::p, RobotsDownloadInterface::c, RobotsDownloadInterface::full;
Dominio * RobotsDownloadInterface::buf[1000];

bool RobotsDownloadInterface::put (Dominio * dominio) {
	// std::cout << "Lock: 033 A" << std::endl;
	boost::mutex::scoped_lock lock(myMutex);
	// std::cout << "Lock: 033 B" << std::endl;
	if (full >= 1000)
		return false;
	else {
		buf[p] = dominio;
		p = (p+1) % 100;
		++full;
		cond.notify_one();
	}
	return true;
}


Dominio * RobotsDownloadInterface::get () {
	// std::cout << "Lock: 034 A" << std::endl;
	boost::mutex::scoped_lock lk(myMutex);
	// std::cout << "Lock: 034 B" << std::endl;
	while (full == 0)
		cond.wait(lk);
	Dominio * dominio = buf[c];
	c = (c+1)%100;
	--full;
	return dominio;
}
